<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="com.smarthome.web.property.utils.UserHolder"  %>
<!DOCTYPE html>
<html>
<head>
<title>小区管理管理</title>
<jsp:include page="../../views/layout/inc.jsp"></jsp:include>
 <script src="/js/jquery-easyui-1.3.3/locale/easyui-rtl.js?v=201505301341"></script>
 <link rel="stylesheet" href="/css/core/easyui/easyui-rtl.css?v=201505241044"/>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/editPage')}"> --%>
	<script type="text/javascript">
		$.canEdit = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/delete')}"> --%>
	<script type="text/javascript">
		$.canDelete = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/grantPage')}"> --%>
	<script type="text/javascript">
		$.canProperty = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/editPwdPage')}"> --%>
	<script type="text/javascript">
		$.canResource= true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/express')}"> --%>
	<script type="text/javascript">
		$.canExpress= true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/laundry')}"> --%>
	<script type="text/javascript">
		$.canLaundry= true;
	</script>
<%-- </c:if> --%>
<script type="text/javascript">
	var dataGrid;
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			url : '/residenceController/dataGrid',
			fit : true,
			fitColumns : false,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'id',
			sortOrder : 'asc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			singleSelect : true,
			nowrap : true,
			rowStyler : function(index,row){
				try{
					if(row.userRole){
						row.userRoleName = row.userRole.name;
					}
					if(row.residenceFee){
						row.showOtherFee = row.residenceFee.showOtherFee;
						row.showBaseFee = row.residenceFee.showBaseFee;
					}
					
					if(!row.showBaseFee || row.showBaseFee == null ){
						row.showBaseFee  = '0.00';
					}
					if(!row.showOtherFee || row.showOtherFee == null ){
						row.showOtherFee  = '0.00';
					}
				} catch(e){}
			},
			//frozenColumns : [ [ ] ],
			columns : [ [ {
				field : 'id',
				title : '编号',
				hidden :true,
				width : 150,
				align:'left',
				checkbox : true
			}, {
				field : 'name',
				title : '小区名字',
				width : 80,
				align:'left',
				sortable : true
			}, {
				field : 'code',
				title : '小区代码',
				width : 80,
				align:'left',
				sortable : true
			} , {
				field : 'areaName',
				title :'区县',
				width : 150,
				align:'left',
				sortable : false
			},{
				field : 'propertyName',
				title : '物业公司',
				align:'left',
				width : 150
			},{
				field : 'propertyAddr',
				title : '物业办公地点',
				width : 150
			},{
				field : 'propertyPhone',
				title : '物业联系方式',
				align:'left',
				width : 150
			},{
				field : 'propertyWorktDate',
				title : '物业工作时间',
				align:'left',
				width : 150
		/**--	},{
				field : 'showBaseFee',
				title : '基本物业费（元/平米/月）',
				align:'left',
				width : 150
			},{
				field : 'showOtherFee',
				title : '其他费用（元/月）',
				align:'left',
				width : 150
				**/
			},{
				field : 'userRoleName',
				title : '角色资源',
				align:'left',
				width : 150
			},{
				field : 'address',
				title :'小区地址',
				width : 150,
				align:'left',
				sortable : false
			},{
				field : 'remark',
				title :'备注',
				width : 150,
				align:'left',
				sortable : false
			},{
				field : 'gmtCreate',
				title : '创建时间',
				width : 150,
				align:'left',
				sortable : true,
				formatter:function(val,rec){ 
					 var unixTimestamp = new Date(val);  
                     return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');  
				}
			}, {
				field : 'gmtModify',
				title : '最后修改时间',
				width : 150,
				sortable : true,
				align:'left',
				formatter:function(val,rec){ 
					 var unixTimestamp = new Date(val);  
                    return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');  
				}
			} ] ],
			frozenColumns : [[{
				field : 'action',
				title : '操作',
				align:'left',
				width : 170,
				formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改"/>', row.id, '/images/extjs_icons/pencil.png');
					}
					str += '&nbsp;';
					if ($.canResource) {
						str += $.formatString('<img onclick="resourceFun(\'{0}\');" src="{1}" title="分配资源"/>', row.id, '/images/extjs_icons/image_link.png');
					}
					str += '&nbsp;';
					if ($.canProperty) {
						str += $.formatString('<img onclick="propertyFun(\'{0}\');" src="{1}" title="指定物业公司"/>', row.id, '/images/extjs_icons/house_link.png');
					}
					/**
					str += '&nbsp;';
					if ($.canProperty) {
						str += $.formatString('<img onclick="propertyFeeFun(\'{0}\');" src="{1}" title="设置物业费"/>', row.id, '/images/extjs_icons/money_yen.png');
					}
					**/
					str += '&nbsp;';
					if ($.canExpress) {
						str += $.formatString('<img onclick="expressFun(\'{0}\',\'{1}\');" src="{2}" title="关联快递点"/>', row.id,row.areaId, '/images/extjs_icons/package_link.png');
					}
					str += '&nbsp;';
					if ($.canLaundry) {
						str += $.formatString('<img onclick="laundryFun(\'{0}\',\'{1}\');" src="{2}" title="关联洗衣店"/>', row.id,row.areaId, '/images/extjs_icons/brick_link.png');
					}
					str += '&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, '/images/extjs_icons/cancel.png');
					}
					return str;
				}
			}]],
			toolbar : '#toolbar',
			onLoadSuccess : function() {
				$('#searchForm table').show();
				parent.$.messager.progress('close');

				$(this).datagrid('tooltip');
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll').datagrid('uncheckAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});

	/**分配资源*/
	function resourceFun(id){
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '分配资源',
			width : 500,
			height : 300,
			href : '/residenceController/dispatchResourcePage?id=' + id,
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
	
	/**指定物业公司*/
	function propertyFun(id){
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '指定物业公司',
			width : 500,
			height : 300,
			href : '/residenceController/dispatchPropertyPage?id=' + id,
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
	
	
	/**指定物业公司*/
	function propertyFeeFun(id){
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '指定物业公司',
			width : 500,
			height : 300,
			href : '/residenceController/dispatchPropertyFeePage?id=' + id,
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function deleteFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前小区？', function(b) {
			if (b) {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.post('/residenceController/delete', {
					id : id
				}, function(result) {
					if (result.success) {
						parent.$.messager.alert('提示', result.msg, 'info');
						dataGrid.datagrid('reload');
					}
					parent.$.messager.progress('close');
				}, 'JSON');
				
			}
		});
	}

	function editFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '修改用户',
			width : 500,
			height : 400,
			href : '/residenceController/editPage?id=' + id,
			buttons : [ {
				text : '确认修改',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//新增新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function addFun() {
		parent.$.modalDialog({
			title : '新增用户',
			width : 500,
			height : 400,
			href : '/residenceController/addPage',
			buttons : [ {
				text : '新增新增',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
	
	function expressFun(id,areaId){
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
			areaId = rows[0].areaId;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '关联快递点',
			width : 500,
			height : 300,
			href : '/residenceController/relResidenceExpress?id=' + id + '&areaId=' + areaId, 
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}
	
	
	function laundryFun(id,areaId){
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
			areaId = rows[0].areaId;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '关联洗衣店',
			width : 500,
			height : 300,
			href : '/residenceController/relResidenceLaundry?id=' + id + '&areaId=' + areaId, 
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',title:'查询条件',border:false" style="height: 110px; overflow: hidden;">
			<form id="searchForm">
				<table class="table table-hover table-condensed" style="display: none;">
					<tr>
						<th>小区名字</th>
						<td><input name="name" placeholder="小区名字" class="span2" /></td>
					</tr>
					<tr>
						<th>小区代码</th>
						<td><input name="code" placeholder="小区代码" class="span2" /></td>
					</tr>
					<tr>
						<th>小区地址</th>
						<td><input name="address" placeholder="小区地址" class="span2" /></td>
					</tr>
					<!-- 
					<tr>
						<th>创建时间</th>
						<td><input class="span2" name="startGmtCreate" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />至<input class="span2" name="endGmtCreate" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" /></td>
					</tr>
					<tr>
						<th>最后修改时间</th>
						<td><input class="span2" name="startGmtModify" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />至<input class="span2" name="endGmtModify" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" /></td>
					</tr>
					 -->
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false">
			<table id="dataGrid"></table>
		</div>
	</div>
	<div id="toolbar" style="display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/addPage')}"> --%>
			<%if(UserHolder.isSuperResidence()){ %>
			<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
			<%} %>
<%-- 		</c:if> --%>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();">提交查询</a>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="cleanFun();">重置查询</a>
	</div>

	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/addPage')}"> --%>
			<%if(UserHolder.isSuperResidence()){ %>
			<div onclick="addFun();" class="easyui-linkbutton" data-options="iconCls:'icon-add'">增加</div>
			<%} %>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/delete')}"> --%>
			<div onclick="deleteFun();" data-options="iconCls:'pencil_delete'">删除</div>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/residenceController/editPage')}"> --%>
			<div onclick="editFun();" data-options="iconCls:'pencil'">修改</div>
<%-- 		</c:if> --%>
	</div>
</body>
</html>